package com.topscomm.mis.cbb.pm.service.abs;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.service.ICboAttachmentService;
import com.topscomm.cbo.service.ICboAttachmentTypeService;
import com.topscomm.cbo.service.ICboUserService;
import com.topscomm.cbo.service.impl.CboAttachmentService;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.mis.cbb.pm.api.IDeliverableService;
import com.topscomm.mis.cbb.pm.api.IProjectService;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmDeliverableEntity;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmDeliverableService;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmProjectService;
import com.topscomm.mis.cbb.pm.base.service.impl.CbbPmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.tap.common.DatacodeCache;
import com.topscomm.tap.common.SessionUserBean;
import java.util.List;
import java.util.Map;

import com.topscomm.tap.threadlocal.ThreadLocalContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.transaction.annotation.Transactional;

@ConditionalOnMissingBean(IDeliverableService.class)
public abstract class AbstractDeliverableService implements IDeliverableService {

    @Autowired
    protected ICbbPmDeliverableService cbbPmDeliverableService;

    @Autowired
    protected ICboAttachmentService attachmentService;

    @Autowired
    protected ICboAttachmentTypeService attachmentTypeService;

    @Autowired
    protected DatacodeCache datacodeCache;

    @Autowired
    protected ICboUserService userService;

    @Autowired
    protected IProjectService projectService;

    /**
     * description 插入数据
     * @author wxm
     * @date 2023-06-25 18:08:22
     * @param paramMap
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @modify
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insert(Map<String, Object> paramMap) {
        beforeInsert(paramMap);
        CbbPmDeliverableEntity deliverableEntity = new CbbPmDeliverableEntity();
        deliverableEntity.convertFromMap(paramMap);
        deliverableEntity = cbbPmDeliverableService.insert(deliverableEntity);
        Map<String, Object> dataMap = deliverableEntity.convertToMap();
        afterInsert(dataMap);
        return dataMap;
    }

    protected void beforeInsert(Map<String, Object> paramMap) throws ServiceException {
    }

    protected void afterInsert(Map<String, Object> paramMap) throws ServiceException {
    }

    /**
     * description 获取列表数据
     * @author wxm
     * @date 2023-06-25 18:08:11
     * @param paraMap
     * @param sqlWhere
     * @return com.topscomm.pub.vo.Page
     * @modify
     */
    @Override
    public Page list(Map<String, Object> paraMap, String sqlWhere) {
        Query query = new Query(paraMap);
        int totalCount = cbbPmDeliverableService.getCount(sqlWhere);
        List<Map<String, Object>> tempList = null;
        if (query.getPageSize() > 0) {
            tempList = cbbPmDeliverableService.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
                query.getSidx(), query.getSord());
        } else {
            tempList = cbbPmDeliverableService.queryMapByWhere(sqlWhere);
        }
        setDispFields(tempList);
        return new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
    }


    protected void setDispFields(List<Map<String, Object>> mapRowList) {
    }

    /**
     * description 更新
     * @author wxm
     * @date 2023-06-25 18:16:33
     * @param paramMap
     * @return void
     * @modify
     */
    @Override
    public void update(Map<String, Object> paramMap) {
        cbbPmDeliverableService.update(paramMap);
    }

    /**
     * description 删除
     * @author wxm
     * @date 2023-06-25 18:20:33
     * @param paraMap
     * @return void
     * @modify
     */
    @Override
    public void delete(Map<String, Object> paraMap) {
        String ids = ConvertUtil.convertToString(paraMap.get("ids"));
        cbbPmDeliverableService.deleteByWhere(" id in (" + ids + ")");
    }

    /**
     * description 加载详情页
     * @author wxm
     * @date 2023-06-25 18:45:12
     * @param paraMap
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @modify
     */
    @Override
    public Map<String, Object> load(Map<String, Object> paraMap) {
        return cbbPmDeliverableService.queryMapById(paraMap.get("id").toString());
    }

    /**
     * description 获取数据权限
     * @author wxm
     * @date 2023-06-25 19:50:39
     * @param select
     * @param tableName
     * @param userBean
     * @return java.lang.String
     * @modify
     */
    @Override
    public String getDataPermission(int select, String tableName, SessionUserBean userBean) {
        return cbbPmDeliverableService.getDataPermission(select,tableName,userBean);
    }

    /**
     * description 根据sourceType和sourceId查询交付物
     *
     * @param sourceType
     * @param sourceId
     * @return java.util.List<com.topscomm.mis.cbb.pm.base.pojo.CbbPmDeliverableEntity>
     * @author wxm
     * @date 2023-06-24 19:49:33
     * @modify
     */
    protected List<CbbPmDeliverableEntity> queryDeliverableBySourceTypeId(String sourceType,
        long sourceId) {
        StringBufferProxy whereSql = new StringBufferProxy();
        whereSql.appendSingle(CbbPmDeliverableEntity.FieldSourcetype + "='{0}'",
            sourceType);
        whereSql.appendSingle(" and " + CbbPmDeliverableEntity.FieldSourceid + "={0}",
            sourceId);
        return cbbPmDeliverableService
            .queryByWhere(whereSql.toString());
    }

    /**
     * description 批量插入
     * @author wxm
     * @date 2023-06-30 20:13:16
     * @param listEntity
     * @return void
     * @modify
     */
    public void insertBatch(List<BasicEntity> listEntity) {
        beforeInsertBatch(listEntity);
        cbbPmDeliverableService.insertBatch(listEntity);
        afterInsertBatch(listEntity);
    }

    protected void afterInsertBatch(List<BasicEntity> listEntity){

    }

    protected void beforeInsertBatch(List<BasicEntity> listEntity){

    }

    public SessionUserBean getSessionUserBean() {
        return ThreadLocalContext.sessionUserBeanThreadLocal.get();
    }
}
